package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Collect;
import com.wm.bean.ResMenu;
import com.wm.bean.Restaurant;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.CollectService;
import com.wm.service.ResMenuService;
import com.wm.service.RestaurantService;
import com.wm.service.RestaurantTimeService;
import com.wm.service.UserService;
import com.wm.service.imp.ResMenuServiceImp;
import com.wm.service.imp.RestaurantServiceImp;
import com.wm.web.servlet.AdminSession;
import com.wm.web.servlet.UserSession;

public class ResAction extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(ResAction.class);
	
	private RestaurantService restaurantService;
	private ResMenuService resMenuService;
	private CollectService collectService;
	private RestaurantTimeService restaurantTimeService;

	public RestaurantTimeService getRestaurantTimeService() {
		return restaurantTimeService;
	}

	public void setRestaurantTimeService(RestaurantTimeService restaurantTimeService) {
		this.restaurantTimeService = restaurantTimeService;
	}

	public CollectService getCollectService() {
		return collectService;
	}

	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}

	public ResMenuService getResMenuService() {
		return resMenuService;
	}

	public void setResMenuService(ResMenuService resMenuService) {
		this.resMenuService = resMenuService;
	}

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	private Map session;
	
	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private String result;
	private long id;
	private Restaurant restaurant;
	private List typeList;
	private long uid;
	private String scrName;
	private long scrId;
	private List timeList;
	private String error;

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public List getTimeList() {
		return timeList;
	}

	public void setTimeList(List timeList) {
		this.timeList = timeList;
	}

	public long getScrId() {
		return scrId;
	}

	public void setScrId(long scrId) {
		this.scrId = scrId;
	}

	public String getScrName() {
		return scrName;
	}

	public void setScrName(String scrName) {
		this.scrName = scrName;
	}
	
	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}
	
	public List getTypeList() {
		return typeList;
	}

	public void setTypeList(List typeList) {
		this.typeList = typeList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public Restaurant getRestaurant() {
		return restaurant;
	}

	public void setRestaurant(Restaurant restaurant) {
		this.restaurant = restaurant;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		if(this.getId()>0){
			this.setRestaurant(this.getRestaurantService().findRestaurantByIdValidIsShow(this.getId(),1,1));
			this.setTimeList(this.getRestaurantTimeService().findRestaurantTimesByResIdIsShow(this.getId(), 1, "startHour", Constant.ORDER_ASC));
			//是否收藏
			if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
				UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
				this.setUid(us.getUid());
				List cl = this.getCollectService().findCollectByUserIdObjTypeFirstResultMaxResults(us.getUid(),0, "id", 0, 0, 10000);
				if(cl!=null){
					for(int i=0;i<cl.size();i++){
						Collect collect = (Collect)cl.get(i);
						if(collect.getObjId() == this.getId() && collect.getObjType() == 0){
							this.getRestaurant().setIsCollect(1);
						}
					}
				}
			}
		
			this.setTypeList(this.getResMenuService().findResMenuResMenuTypeByResId(this.getId()));
		}
		if(this.getError()!=null&&this.getError().equals("1")){
			this.addFieldError("error", this.getText("error.shopping.null"));
		}
		if(this.getError()!=null&&this.getError().equals("2")){
			this.addFieldError("error", this.getText("error.delivery.price"));
		}

//			int num = 20;
//			for(int i=0;i<num;i++){
//				ResMenu r = new ResMenu();
//				//r.setId(i+2);
//				r.setAddTime(new Date());
//				r.setBuyNum(10);
//				r.setDiscount(5);
//				r.setIsFacia(0);
//				r.setIsPromotion(0);
//				r.setIsShow(1);
//				r.setMenuAnglic("cm"+i);
//				r.setMenuInitials("D");
//				r.setMenuMemo("这里是备注");
//				r.setMenuName("菜名"+i);
//				r.setMenuPhonetic("caiming"+i);
//				r.setOrderNum(i);
//				r.setOrderTime(new Date());
//				r.setPrice(10);
//				r.setResId(11111);
//				r.setScore(4);
//				r.setScorePeople(100);
//				if(i<5){
//					r.setTypeId(1);
//				}else if(i>5 &&i<10){
//					r.setTypeId(2);
//				}else if(i>10 &&i<15){
//					r.setTypeId(3);
//				}else if(i>15 &&i<20){
//					r.setTypeId(4);
//				}
//				
//				this.getResMenuService().saveResMenu(r);
//			}
		return "index";
	}
}
